package com.itheima.dao.impl;

import com.itheima.dao.ItemDao;
import com.itheima.model.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * @Author：shenkunlin
 * @date： 2021/7/5 11:50
 * @Description：
 ***/
@Repository
public class ItemDaoImpl implements ItemDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 削减库存
     * @param id
     * @param count
     * @return
     */
    @Override
    public int decrCount(Integer id, Integer count) {
        //sql
        String sql = "update item set count=count-? where id=?";
        //执行修改
        return jdbcTemplate.update(sql,count,id);
    }

    /**
     * 获取单价
     * @param id
     * @return
     */
    @Override
    public int getPrice(Integer id) {
        //sql
        String sql = "select price from item where id=?";
        return jdbcTemplate.queryForObject(sql,Integer.class,id);
    }

    /**
     * 根据ID查询商品详情
     * @param id
     * @return
     */
    @Override
    public Item one(Integer id) {
        //sql
        String sql = "select * from item where id=?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Item>(Item.class),id);
    }
}
